var say = say||{};
say.classification = say.classification||{};
say.classification.index = say.classification.index||{};

//页面渲染
say.classification.index.render = function(){
	var url = "/say/classification/rest/query";
	$.ajax({
		type: 'POST',
		url: url,
       	data: {},
       	dataType:'json',	
       	success: function (result) {	
	   		if(result && result.status){
				if(result.status==1 && result.data){
					var attrId = "";
					$("#CLASS_NODE").empty();
					$.each(result.data,function(index,item){
						if(index==0){
							attrId = item.id;
						}
						$("#CLASS_NODE").append(_.template($("#CLASS_ITEM").html())({
							index: index,
							info: item
						}));
					});
					$("#CLASS_NODE").append($("#ADD_CLASS_ITEM").html());
					$("#artwork").load("/say/artwork/index?id="+attrId);
				}else{
					$("#CLASS_NODE").append($("#ADD_CLASS_ITEM").html());
				}
			}else{
				layer.msg("未知错误",{icon:0});
			}
       	},
       	error: function(result){
       		alert("未知错误");
       	}
	});
}

//新增分类
say.classification.index.add = function(){
	var url = "/say/classification/rest/save";
	var formData = base.serializeObject($("#addForm"));
	$.ajax({
		type: 'POST',
		url: url,
		data: JSON.stringify(formData),
       	dataType: 'json',	
       	contentType: 'application/json',
       	success: function (result) {	
	   		if(result && result.status){
				if(result.status==1){
					layer.msg("新增申请成功，等待审核",{icon:1});
					layer.close(say.classification.index.layerIndex);
					say.classification.index.render();
				}else if(result.status==0){
					layer.msg(result.msg,{icon:0});
				}
			}
       	},
       	error: function(result){
       		layer.msg("未知错误",{icon:0});
       	}
	});
}

//分类点击事件
say.classification.index.navClick = function(){
	var flag = $(this).attr("class");
	var attrId = $(this).attr("attrId");
	if(!(flag && flag=="add-classification")){
		$(".nav.nav-pills li").removeClass("active");
		$(this).addClass("active");
		$("#artwork").load("/say/artwork/index?id="+attrId);
	}
}

//新增分类
say.classification.index.addClassification = function(){
	say.classification.index.layerIndex = layer.open({
        type: 1,
        title: "请填写分类名称",
        area: ["300px", "200px"],
        content: $("#ADD_CLASSIFICATION").html(),
        success: function(index){
        	$("#addBtn").click(say.classification.index.add);
        	$("#cancel").click(function(){
        		layer.close(say.classification.index.layerIndex);
        	});
        }
	});
}


$(function(){
	say.classification.index.render();
	
	
	//绑定事件
	$("#CLASS_NODE").on("click","li",say.classification.index.navClick);	//导航条点击事件
	$("#CLASS_NODE").on("click",".add-classification",say.classification.index.addClassification);	//新增
	
});
